/**
 * 
 */
package com.hfjyz.exam.controller;

import java.io.File;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.jxls.transformer.XLSTransformer;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.hfjyz.exam.command.ExamSearchCommand;
import com.hfjyz.exam.service.ExamService;

/**
 *
 */
public class ExamSearchController extends SimpleFormController {
	private ExamService examService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		HttpSession session = request.getSession();
		Object cmd = session.getAttribute(getFormSessionAttributeName(request));
		if (cmd != null) {
			return cmd;
		}
		return super.formBackingObject(request);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#referenceData
	 * (javax.servlet.http.HttpServletRequest, java.lang.Object,
	 * org.springframework.validation.Errors)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		ExamSearchCommand cmd = (ExamSearchCommand) command;
		return examService.initSearch(request, cmd, errors);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ExamSearchCommand cmd = (ExamSearchCommand) command;
		request.getSession().setAttribute(getFormSessionAttributeName(), cmd);
		Map model = errors.getModel();
		search(model, request, cmd);
		if (request.getParameter("excelExport") != null) {
			File dest = File.createTempFile("excelExport", ".xlsx");
			dest.deleteOnExit();
			String src = request.getSession().getServletContext()
					.getRealPath("/WEB-INF/template/excelExport.xlsx");
			XLSTransformer transformer = new XLSTransformer();
			transformer.transformXLS(src, model, dest.getAbsolutePath());
			String uuid = UUID.randomUUID().toString();
			request.getSession().setAttribute(uuid, dest.getAbsolutePath());
			model.put("downloadFile", uuid);
		} else {
			model.remove("downloadFile");
		}
		return new ModelAndView(getFormView(), model);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void search(Map model, HttpServletRequest request,
			ExamSearchCommand cmd) throws Exception {
		examService.search(request, cmd);
		model.put("exams", cmd.getResult());
	}

	/**
	 * @return the examService
	 */
	public ExamService getExamService() {
		return examService;
	}

	/**
	 * @param examService
	 *            the examService to set
	 */
	public void setExamService(ExamService examService) {
		this.examService = examService;
	}

}
